//
//  DDTabbarView.swift
//  DYSwiftFramework
//
//  Created by huangdeyu on 2021/10/20.
//  Copyright © 2021 none. All rights reserved.
//

import Foundation
import UIKit
import SnapKit


// 所有自定义实现的tabbar必须实现这个协议
protocol DDTabbarItemProtocol where Self : UIView {
    var didClickedBlock:(()->Void)? { get set }
    func setSelected(selected:Bool)
}


class DDTabbarItem: UIView,DDTabbarItemProtocol {
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    init(title:String,titleColor:UIColor,titleSelectedColor:UIColor,image:UIImage,selectedImage:UIImage) {
        self.title = title
        self.titleColor = titleColor
        self.titleSelctedColor = titleSelectedColor
        self.image = image
        self.selectedImage = selectedImage
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.white
        setup()
    }
    
    private var title = ""
    private var titleColor:UIColor?
    private var titleSelctedColor: UIColor?
    private var image:UIImage?
    private var selectedImage:UIImage?
    
    
    
    private var imgView:UIImageView?
    private var titleLbl:UILabel?
    private var selected:Bool = false
    
    var didClickedBlock:(()->Void)?

    private func setup() {
        imgView = UIImageView.init()
        imgView?.image = image
        addSubview(imgView!)
        
        titleLbl = UILabel.init()
        titleLbl?.textColor = titleColor
        titleLbl?.text = title
        titleLbl?.font = UIFont.systemFont(ofSize: 10)
        addSubview(titleLbl!)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(onTapped))
        addGestureRecognizer(tap)
        
        imgView?.snp.makeConstraints({ (make) in
            make.width.equalTo(24)
            make.height.equalTo(24)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
        })
        titleLbl?.snp.makeConstraints({ (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.imgView!.snp.bottom)
            make.bottom.equalToSuperview().inset(6)
        })
        
    }
    
    @objc private func onTapped(){
        // 已经被选中了，阻止点击
        if selected {
            return
        }
        if let block = self.didClickedBlock {
            block()
        }
    }
    
    func setSelected(selected:Bool)  {
        self.selected = selected
        // 如果被选中
        if self.selected {
            imgView?.image = selectedImage
            titleLbl?.textColor = titleSelctedColor
        } else {
            imgView?.image = image
            titleLbl?.textColor = titleColor
        }
    }
}




class DDTabbarView: UIView {
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    init(items:[DDTabbarItemProtocol], selectedIndex:Int = 0) {
        self.items = items
        self.selectedIndex = selectedIndex
        super.init(frame: CGRect.zero)
        setup()
    }
    
    
    var didSelctedIndex:((Int)->Void)?;
    private var items:[DDTabbarItemProtocol] = []
    var selectedIndex = 0;
    private var stackView:UIStackView = UIStackView.init()
    
    
    
    private func setup() {
        self.backgroundColor = UIColor.white
        stackView.axis = .horizontal
        stackView.spacing = 0
        stackView.distribution = .fillEqually
        stackView.alignment = .center
        addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview().inset(0)
            make.height.equalTo(49)
        }
        for (index,item) in items.enumerated() {
            item.didClickedBlock = {[weak self] ()->Void in
                if let block = self?.didSelctedIndex {
                    block(index)
                }
            }
            if selectedIndex == index {
                item.setSelected(selected: true)
            }
            stackView.addArrangedSubview(item)
        }
        let topLine = UIView.init()
        topLine.backgroundColor = UIColor.init(hex: 0xf2f2f2)
        addSubview(topLine)
        topLine.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview().inset(0)
            make.height.equalTo(0.5)
        }
    }
    
    
    func setSelectedIndex(index:Int) -> Void {
        if selectedIndex == index {
            return
        }
        self.items.forEach({ (value) in
            value.setSelected(selected: false)
        })
        self.items[index].setSelected(selected: true)
        selectedIndex = index
    }
}
